package demisemapping;

public class Location implements Comparable, Cloneable {
    int k, i, j;
    
    public Location(String loc) {
        String[] tokens = loc.split(",");
        if (tokens.length == 3) {
            j = Integer.parseInt(tokens[0]);
            i = Integer.parseInt(tokens[1]);
            k = Integer.parseInt(tokens[2]);
        } else {
            j = i = k = -1;
        }
    }
    
    public Location(int k, int i, int j) {
        this.k = k;
        this.i = i;
        this.j = j;
    }
    
    // manhattan distance between 2 locations
    public int dist(Location o) {
        return Math.abs(o.k-k)+Math.abs(o.i-i)+Math.abs(o.j-j);
    }
    
    public String toString() {
        return j+","+i+","+k;
    }
    
    public int compareTo(Object o) {
        assert(o instanceof Location);
        Location loc = (Location) o;
        if (k < loc.k) return -1;
        if (k > loc.k) return 1;
        if (i < loc.i) return -1;
        if (i > loc.i) return 1;
        if (j < loc.j) return -1;
        if (j > loc.j) return 1;
        return 0;
    }
    
    @Override
    public boolean equals(Object obj) {
        if (!(obj instanceof Location)) return false;
        return compareTo(obj) == 0;
    }
    
    @Override
    public Object clone() {
        return new Location(k, i, j);
    }
}
